package org.flower.web.servlet;

import org.flower.dao.CollectDao;
import org.flower.dao.ShoppingCartDao;
import org.flower.dao.impl.CollectDaoImpl;
import org.flower.dao.impl.ShoppingCartDaoImpl;
import org.flower.entity.ShoppingCart;
import org.flower.entity.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns="/toShoping.shtml")
public class toShopingServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    /**
     * 去添加购物车的servlet
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理字符集编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        //获取登录的该用户
        User u = (User)request.getSession().getAttribute("user");
        //获取页面参数
        String flag = request.getParameter("doWhat");
        if("checkLogin".equals(flag)){
            //验证用户是否登录
            if(u==null){
                //没有用户登录 不可进行下一步操作
                out.print(false);
            }else{
                out.print(true);
            }
        }else if("addShoping".equals(flag)){
            //添加购物车
            ShoppingCartDao shdao = new ShoppingCartDaoImpl();
            //获取商品编号
            String id = request.getParameter("id");
            //获取用户编号
            int uid = u.getId();
            int comId = Integer.parseInt(id);
            //查询 该用户的该商品 有没有添加过
            boolean isAdd = shdao.getisExistByComIdAndUid(uid,comId);
            //如果该用户添加过该商品 该商品数量+1  如果没有添加过 则添加该商品信息
            if(isAdd){
                shdao.updateShoppingNum(uid,comId,1);
            }else{
                //创建购物车对象
                ShoppingCart shopping = new ShoppingCart();
                shopping.setUserId(uid);
                shopping.setComDetailsId(comId);
                shdao.addShoppingCart(shopping);
            }
            //查询该用户的订单数量 存放到session中
            int num = shdao.getShoppingCountNum(uid);
            request.getSession().setAttribute("userShoppingCartNum",num);
            //将 新的购物车数量 打回去
            out.print(num);
        }else if("goShoppingCart".equals(flag)){
            //去购物车
            String path = request.getContextPath();
            //如果该用户没有登录 让他去登录页面
            if(null==u){
                //重定向到 登录页面
                response.sendRedirect(path+"/login.jsp");
                return;
            }
            //获取用户的购物车数量 如果为0 让他去空的购物车页面
            ShoppingCartDao shdao = new ShoppingCartDaoImpl();
            int num = shdao.getShoppingCountNum(u.getId());
            if(num>0){
                response.sendRedirect(path+"/user/shoppingCart.shtml");
            }else{
                response.sendRedirect(path+"/user/shoppingTrolley.jsp");
            }
        }else if("addCollect".equals(flag)){
            //收藏商品
            //获取商品编号
            String idStr = request.getParameter("id");
            int id = Integer.parseInt(idStr);
            CollectDao cdao = new CollectDaoImpl();
            out.print(cdao.addCollectByUidAndComId(u.getId(),id));
        }else if("deleteCollect".equals(flag)){
            //删除收藏的商品
            //获取商品编号
            String idStr = request.getParameter("id");
            int id = Integer.parseInt(idStr);
            CollectDao cdao = new CollectDaoImpl();
            out.print(cdao.deleteCollectByUidAndComId(u.getId(),id));
        }


    }
}
